Grokking LinkedIn Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
LinkedIn Easy
Find Pivot Index (easy)
Solution: Find Pivot Index
Isomorphic Strings (easy)
Solution: Isomorphic Strings
Second Minimum Node In a Binary Tree (easy)
Solution: Second Minimum Node In a Binary Tree
Minimum Time to Type Word Using Special Typewriter (easy)
Solution: Minimum Time to Type Word Using Special Typewriter
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Can Place Flowers (easy)
Solution: Can Place Flowers
Valid Perfect Square (easy)
Solution: Valid Perfect Square
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Sqrt (easy)
Solution: Sqrt
Valid Palindrome (easy)
Solution: Valid Palindrome
Rotate List (medium)
Solution: Rotate List
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Valid Triangle Number (medium)
Solution: Valid Triangle Number
Valid Parenthesis String (medium)
Solution: Valid Parenthesis String
Edit Distance (medium)
Solution: Edit Distance
Remove All Ones With Row and Column Flips II (medium)
Solution: Remove All Ones With Row and Column Flips II
Reverse Integer (medium)
Solution: Reverse Integer
Triplet Sum to Zero (medium)
Solution: Triplet Sum to Zero
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Decode Ways (medium)
Solution: Decode Ways
Quadruple Sum to Target (medium)
Solution: Quadruple Sum to Target
Next Permutation (medium)
Solution: Next Permutation
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Word Search (medium)
Solution: Word Search
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
House Robber II (medium)
Solution: House Robber II
Coin Change
Solution: Coin Change
Design Add and Search Words Data Structure (medium)
Solution: Design Add and Search Words Data Structure
Merge Intervals (medium)
Solution: Merge Intervals
Number of Islands (easy)
Solution: Number of Islands
Longest Palindromic Subsequence (medium)
Solution: Longest Palindromic Subsequence
Kth Smallest Number (medium)
Solution: Kth Smallest Number
Maximum Product Subarray (medium)
Solution: Maximum Product Subarray
Same Tree (medium)
Solution: Same Tree
Max Points on a Line (hard)
Solution: Max Points on a Line
Closest Binary Search Tree Value II (hard)
Solution: Closest Binary Search Tree Value II
Word Ladder (hard)
Solution: Word Ladder
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Solution: Isomorphic Strings

Problem Statement

Given two strings s and t, return true if they are isomorphic. Otherwise, return false.

Two strings s and t are called isomorphic if the characters in string s can be replaced to get string t.

Each character in string t should be replaced with another character while preserving the order of characters as in string s. Two characters should not map to the same character, but a character may map to itself.

Examples

  • Example 1:

    • Input: s = "abb", t = "cdd"
    • Expected Output: true
    • Justification: Each character in "abb" can be replaced to match the corresponding character in "cdd" (a->c, b->d) while maintaining a one-to-one mapping.
  • Example 2:

    • Input: s = "cbcrt", t = "abaxv"
    • Expected Output: true
    • Justification: The characters in "cbcrt" can be replaced to form "abaxv" with a unique mapping for each character (c->a, b->b, r->x, t->v).
  • Example 3:

    • Input: s = "abcd", t = "bbcd"
    • Expected Output: false
    • Justification: The first string's characters cannot be replaced to form the string t while maintaining a unique mapping, as 'b' in the second string corresponds to both 'a' and 'b' in the s.

Solution

To solve this problem, we'll use a mapping strategy to track the transformation of characters from the first string to the second. This approach works by ensuring that each character in the first string can uniquely map to a character in the second string, and vice versa. The reason this method is effective lies in its ability to maintain a one-to-one correspondence between the characters of both strings, which is crucial for the strings to be isomorphic. By using two hash maps (or dictionaries) to store these mappings in both directions, we can efficiently check the conditions for isomorphism. This dual-mapping technique allows us to verify that no two characters in the first string map to the same character in the second string and that the mapping is consistent throughout both strings.

Step-by-step Algorithm

  1. Initialize Two Hash Maps: Start by creating two empty hash maps (or dictionaries). The first map (mapS2T) will store mappings from characters in string s to string t. The second map (mapT2S) will store mappings in the opposite direction, from characters in string t to string s.

  2. Iterate Over Characters: Loop through each character of the strings s and t simultaneously. For each position i, get the characters charS from s and charT from t.

  3. Check and Update Mappings:

    • For each character pair (charS, charT), perform the following checks:
      • If charS is already mapped in mapS2T, check if it maps to the current charT. If not, the strings are not isomorphic, return false.
      • Similarly, if charT is already mapped in mapT2S, check if it maps to the current charS. If not, return false.
    • If both checks pass, update mapS2T by mapping charS to charT and mapT2S by mapping charT to charS.
  4. Return True: If the loop completes without finding any inconsistencies in the mappings, return true. This means that the strings are isomorphic.

Algorithm Walkthrough

Let's consider the input: s = "cbcrt", t = "abaxv"

  • Initial State: mapS2T = {}, mapT2S = {}

  • Step 1: (c, a)

    • mapS2T does not contain c, and mapT2S does not contain a.
    • Update maps: mapS2T = {c: a}, mapT2S = {a: c}
  • Step 2: (b, b)

    • mapS2T does not contain b, and mapT2S does not contain b.
    • Update maps: mapS2T = {c: a, b: b}, mapT2S = {a: c, b: b}
  • Step 3: (c, a)

    • mapS2T contains c mapping to a, and mapT2S contains a mapping to c. The mappings are consistent.
    • No update needed. Maps remain: mapS2T = {c: a, b: b}, mapT2S = {a: c, b: b}
  • Step 4: (r, x)

    • mapS2T does not contain r, and mapT2S does not contain x.
    • Update maps: mapS2T = {c: a, b: b, r: x}, mapT2S = {a: c, b: b, x: r}
  • Step 5: (t, v)

    • mapS2T does not contain t, and mapT2S does not contain v.
    • Update maps: mapS2T = {c: a, b: b, r: x, t: v}, mapT2S = {a: c, b: b, x: r, v: t}
  • Final State: After iterating through all character pairs, the mappings in both mapS2T and mapT2S are consistent and complete without any conflicts. Thus, return true.

Image

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity: - where N is the length of the strings. The algorithm iterates over each character of the strings exactly once to check or create a mapping.

Space Complexity: - The space used by the hash maps does not depend on the size of the input strings directly since the number of unique characters that can be stored is limited by the character set (e.g., ASCII has 128 unique characters, extended ASCII has 256). Therefore, the space complexity can be considered as in terms of the input size.

Isomorphic Strings (easy)
Second Minimum Node In a Binary Tree (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis